home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2678 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  71 lines

  1. Path: news2.aimnet.com!usenet
  2. From: Dan Howard <dhoward@kset.com>
  3. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.tools.mfc
  4. Subject: Re: Newbie question: default op=
  5. Date: 19 Jan 1996 01:16:46 GMT
  6. Organization: Aimnet Information Services
  7. Message-ID: <4dmrdu$niv@news2.aimnet.com>
  8. References: <30FEA0B4.4F66@ymi.com>
  9. NNTP-Posting-Host: host110.kset.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  14.  
  15. Sadly, you are correct.  You do need to write an explicit operator =() because the compiler 
  16. does not provide one.  Yes, the compiler should be able to figure it out but it isn't in the 
  17. Draft ANSI C++ Specification so too bad.
  18.  
  19. If you want an even worse problem, the copy constructor automatically provided by compilers 
  20. does a bit-copy rather than a member-by-member copy.  Thus, no copy constructors are called in 
  21. the following code:
  22.  
  23. class stupid
  24.    {
  25.    CString x;
  26.    };
  27.  
  28. void main()
  29. {
  30.    stupid a;
  31.    
  32.    stupid b(a);  // try stepping into this line, no CString::CString(const CString& s) called
  33.  
  34.    return;
  35. }
  36.  
  37. That's the ANSI spec.  That is why it is always recommended that you declare a copy 
  38. constructor.  If you declare and don't define it, the linker will complain.  If you don't 
  39. declare or define it, the linker will substitute a bit-copy and (sometimes) confuse the shit 
  40. out of you for several hours.
  41.  
  42. Jeffrey Keays <keays@ymi.com> wrote:
  43. >I am trying to use the &*%$ MFC collections in some way that will dispose of its contents on destruction.  In 
  44. >my 2-days and counting of ramming my head repeatedly against this brick wall over something that should be made 
  45. >'easier' by them, I have this general C++ question.
  46. >
  47. >Why is it making me create operator= functions for assignment of the same type (or instance / reference of the 
  48. >same type).  The class I'm trying to assign has no pointers or anything that would require special handling.  
  49. >Why do I have to write stupid things like
  50. >
  51. >class foo {
  52. >    int data1;
  53. >    double data2;
  54. >    CString data3;
  55. >    // ... ad infinitum
  56. >public:
  57. >    foo& operator=(const foo &);
  58. >}
  59. >
  60. >foo& foo::operator=(const foo &rhs)
  61. >{
  62. >    data1 = rhs.data1
  63. >    data2 = rhs.data2
  64. >    data3 = rhs.data3
  65. >    // ... -- I should not have to write this, it should be ASSUMED 
  66. >    //        by the compiler if I don't specify otherwise.
  67. >}
  68. >
  69.  
  70.  
  71.